using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace CollegeShop.Infrastructure.Entities.Repository
{
    [Table("T_Deliveryaddress")]
    public class T_Deliveryaddress : IEntity<T_Deliveryaddress>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(128)]
        public string UserName { get; set; }

        public int SalesUnitID { get; set; }

        [Column(TypeName="bit")]
        public bool isDefault { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(128)]
        public string Address { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(64)]
        public string LinkMan { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(64)]
        public string LinkPhone { get; set; }

        public override T_Deliveryaddress Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.ID == id);
        }

        public override T_Deliveryaddress Find(System.Linq.Expressions.Expression<Func<T_Deliveryaddress, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Deliveryaddress.FirstOrDefault(where);
                if (item != null)
                {
                    this.ID = item.ID;
                    this.UserName = item.UserName;
                    this.SalesUnitID = item.SalesUnitID;
                    this.isDefault = item.isDefault;
                    this.Address = item.Address;
                    this.LinkMan = item.LinkMan;
                    this.LinkPhone = item.LinkPhone;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_Deliveryaddress, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Deliveryaddress.Any(where);
            }
        }
    }
}
